package com.winter.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.winter.mapper.UserMapper;
import com.winter.model.User;
import com.winter.service.UserService;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
/**
 * Created by chris_gang on 2018/4/11.
 */
@Service(value = "userService")
@CacheConfig(cacheNames = "common")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    /*
     * 这个方法中用到了我们开头配置依赖的分页插件pagehelper
     * 很简单，只需要在service层传入参数，然后将参数传递给一个插件的一个静态方法即可；
     * pageNum 开始页数
     * pageSize 每页显示的数据条数
     * */
//    @Override
//    public List<User> findAllUser(int pageNum, int pageSize) {
////        //将参数传给这个方法就可以实现物理分页了，非常简单。
//        PageHelper.startPage(pageNum, pageSize);
//
//        return userMapper.selectAllList();
//    }

    @Override
    @Cacheable(key ="#userId")
    public User selectById(String userId) {
        return userMapper.selectById(Integer.parseInt(userId));
    }

    public static void main(String[] args) {
        // 创建一个与服务器的连接
        try {
            ZooKeeper zk = new ZooKeeper("192.168.101.10:2181",
                    5000, new Watcher() {
                // 监控所有被触发的事件
                public void process(WatchedEvent event) {
                    System.out.println("已经触发了" + event.getType() + "事件！");
                }
            });

            System.out.println(zk.getState());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
